package org.rendao.index
{
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	import org.rendao.entity.FirstIndex;
	import org.rendao.service.FirstIndexService;
	import org.rendao.service.SecondIndexService;

	public class IndexViewPM
	{
		public var dm:IndexViewDM;
		
		public function getFirstIndexList():void {
			var onGet:Function = function(list:Array):void {
				dm.firstIndexList = new ArrayCollection(list);
			};
			FirstIndexService.instance.getAll(onGet);
		}
		
		public function getFirstIndex(id:String):void {
			var onGet:Function = function(result:FirstIndex):void {
				dm.firstIndex = result;
			};
			FirstIndexService.instance.getById(id, onGet);
		}
		
		public function saveFirstIndex():void {
			var onSave:Function = function(result:FirstIndex):void {
				dm.firstIndex = result;
				getFirstIndexList();
				Alert.show("保存成功");
			};
			FirstIndexService.instance.save(dm.firstIndex, onSave);
		}
		
		public function addFirstIndex():void {
			dm.firstIndex = new FirstIndex();
			dm.secondIndexList = new ArrayCollection();
		}
		
		public function deleteFirstIndex():void {
			var onDelete:Function = function():void {
				dm.firstIndex = new FirstIndex();
				dm.secondIndexList = new ArrayCollection();
				getFirstIndexList();
				Alert.show("刪除成功");
			};
			FirstIndexService.instance.remove(dm.firstIndex.id, onDelete);
		}
		
		public function getSecondIndexList(firstIndexId:String):void {
			var onGet:Function = function(list:Array):void {
				dm.secondIndexList = new ArrayCollection(list);
			};
			SecondIndexService.instance.getByFirstIndex(firstIndexId, onGet);
		}
		
		public function deleteSecondIndex(id:String):void {
			var onDelete:Function = function():void {
				getSecondIndexList(dm.firstIndex.id);	
			};
			SecondIndexService.instance.remove(id, onDelete);
		}
	}
}